/**
  ETFRoot project, Anyang Normal University && IMP-CAS
  \class ETFVMEEvent
  \brief class for holding an event for VME daq
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2021-09-27
  \date 2021-09-28 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date        <th>Author      <th>Description                    </tr>
  <tr>  <td>2021-09-27   <td>Asia Sun    <td>file created                   </tr>
  </table>

  \copyright Copyright (c) 2021 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

#include <TClonesArray.h>
#include "FairEventHeader.h"
#include "ETFVMEEvent.h"
#include "ETFHPTDCChannel.h"
#include "ETFAmplitudeChannel.h"
#include "ETFMsg.h"

ClassImp(ETFVMEEvent);

ETFVMEEvent::ETFVMEEvent() : TNamed("VMEEvent", "ETFVMEEvent")
  ,fBunchID(-1), fEventID(-1) ,fHPTDCChannels(new TClonesArray("ETFHPTDCChannel"))
  ,fAmplitudeChannels(new TClonesArray("ETFAmplitudeChannel")){
  fSca.fill(0); fPSca.fill(0); fDSca.fill(0);
} // end ctor

ETFVMEEvent::ETFVMEEvent(const ETFVMEEvent &evt): TNamed(evt),
    fEventHead(evt.fEventHead), fBunchID(evt.fBunchID), fEventID(evt.fEventID),
    fSca(evt.fSca), fPSca(evt.fPSca), fDSca(evt.fDSca),
    fHPTDCChannels(new TClonesArray(*evt.fHPTDCChannels)),
    fAmplitudeChannels(new TClonesArray(*evt.fAmplitudeChannels))
{}

ETFVMEEvent::~ETFVMEEvent(){
  if(fHPTDCChannels){
    fHPTDCChannels->Delete();
    delete fHPTDCChannels;
    fHPTDCChannels = nullptr;
  }
  if(fAmplitudeChannels){
    fAmplitudeChannels->Delete();
    delete fAmplitudeChannels;
    fAmplitudeChannels = nullptr;
  }
} // end the destructor

void ETFVMEEvent::Reset(){
  fHPTDCChannels->Clear();
  fAmplitudeChannels->Clear();
  fEventHead.initialize();
} // end member function Reset

TClonesArray *ETFVMEEvent::GetHPTDCChannels() const{
  if(!fHPTDCChannels)
    ETFMsg::Error("ETFVMEEvent", "GetHPTDCChannels: required pointer is null");
  return fHPTDCChannels;
} // end the member function GetHPTDCChannels

TClonesArray *ETFVMEEvent::GetAmplitudeChannels() const{
  if(!fAmplitudeChannels)
    ETFMsg::Error("ETFVMEEvent", "GetAmplitudeChannels: required pointer is null");
  return fAmplitudeChannels;
} // end the member function GetAmplitudeChannels

void ETFVMEEvent::PrintBunchID() const{
  cout << "event: " << fEventHead.index;
  cout << "\tbunchID: " << fBunchID << endl;
} // end member function PrintBunchID
void ETFVMEEvent::PrintEventID() const{
  cout << "event: " << fEventHead.index;
  cout << "\tEventID: " << fEventID << endl;
} // end member function PrintEventID
void ETFVMEEvent::Print() const{
  fEventHead.print();
  cout << "BunchID: " << fBunchID << "\tEventID: " << fEventID << endl;

  cout << " ----------- SCA ------------" << endl;
  for(int j = 0; j < 16; j++){ // DEBUG
    cout << "sca[" << j << "]: " << fSca[j]; // DEBUG
    cout << "\tpsca[" << j << "]: " << fPSca[j]; // DEBUG
    cout << "\tdsca[" << j << "]: " << fDSca[j] << endl; // DEBUG
  } // end for over i

  ETFRawChannel *ch;
  TIter nextT(fHPTDCChannels), nextA(fAmplitudeChannels);
  // don't use bunchid for the relative time here. See the header file for the explanation.
  while((ch = dynamic_cast<ETFRawChannel *>(nextT()))) ch->Print();
  while((ch = dynamic_cast<ETFRawChannel *>(nextA()))) ch->Print();

  cout << "tch_num: " << fHPTDCChannels->GetEntries();
  cout << "\tach_num: " << fAmplitudeChannels->GetEntries() << endl;
} // end member function Print
